﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.DTO;
using Common.DTO.ihopplockDTO;

namespace DataLayer
{
    public class InGameRepository : RepositoryBase
    {


        public GameRoundEntity GetLatestGameRound(int gameId)
        {
            var gameRounds = this.GetEntitiesWhere<GameRoundEntity>(x => x.GameID == gameId);
            var latestGameRound = gameRounds.OrderByDescending(x => x.Date).FirstOrDefault();

            return latestGameRound;
        }

        public PlayerChoiceEntity GetOpponentChoiceOfLatestRound(int gameId, int playerAskingId)
        {
            var latestRound = this.GetLatestGameRound(gameId);
            foreach (var playerChoice in latestRound.PlayerChoice)
	        {
                if (playerChoice.PlayerID == playerAskingId)
                {
                    continue;
                }
                return playerChoice;
	        }

            return null;
        }

        public bool SaveGameRound(int gameId)
        {
            var gameRound = new GameRoundEntity
            {
                GameID = gameId,
                Date = DateTime.Now 
            };
            return this.Save<GameRoundEntity>(gameRound);
        }
        
    }
}
